package com.wuhan.template.module.home.activity;

import android.content.Intent;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseActivity;
import com.wuhan.template.constant.Urls;
import com.wuhan.template.module.home.adapter.JiaoYuActivityBaseAdapter;
import com.wuhan.template.module.home.adapter.JiaoYuActivityPageAdapter;
import com.wuhan.template.module.home.entity.JiaoYuActivityEntity;
import com.wuhan.template.module.home.listener.MyViewPageListener;
import com.wuhan.template.utils.EventMessage;
import com.wuhan.template.utils.OkHttpUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/12 0012.
 */
public class JiaoYuHuoDong extends BaseActivity{

    @BindView(R.id.jiaoyu_Huodong_page)
    protected ViewPager viewPager;

    @BindView(R.id.jiaoyu_Huodong_listView)
    protected PullToRefreshListView listView;

    @BindView(R.id.jiaoyu_backImage)
    protected ImageView imageView;

    @BindView(R.id.page_title)
    protected TextView textTitle;

    private Handler handler = new Handler();
    private int i =0;
    private List<JiaoYuActivityEntity.ListBean> listBeans =new ArrayList<>();
    private JiaoYuActivityBaseAdapter baseAdapter;

    private int offset=0;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_jiaoyu_huodong;
    }

    @Override
    protected void initViews() {

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                viewPager.setCurrentItem(++i);
                handler.postDelayed(this, 2500);
            }
        }, 2500);


        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(JiaoYuHuoDong.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });


        baseAdapter = new JiaoYuActivityBaseAdapter(this, listBeans);
        listView.setAdapter(baseAdapter);

        listView.setMode(PullToRefreshBase.Mode.BOTH);
        final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                long timeMillis = System.currentTimeMillis();
                String time = format.format(timeMillis);
                listView.getLoadingLayoutProxy().setRefreshingLabel("正在刷新");
                listView.getLoadingLayoutProxy().setPullLabel("下拉刷新");
                listView.getLoadingLayoutProxy().setReleaseLabel("释放开始刷新");

                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel("最后更新时间:" + time);
                listBeans.clear();
                offset=0;
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                listView.getLoadingLayoutProxy().setRefreshingLabel("正在玩命加载");
                listView.getLoadingLayoutProxy().setPullLabel("加载更多");
                listView.getLoadingLayoutProxy().setReleaseLabel("释放加载更多");

                offset+=20;
                loadData();

            }
        });


    }

    @Override
    protected void loadData() {
        OkHttpUtils.get(Urls.JIAOYUHUODONG+"&offset="+offset+"&slide=1&num=5",null);
    }

    @Override
    protected void postEventResult(@NonNull EventMessage message) {
        if(message.type.equals(Urls.JIAOYUHUODONG+"&offset="+offset+"&slide=1&num=5")){
            Gson gson = new Gson();
            JiaoYuActivityEntity jiaoYuActivityEntity = gson.fromJson(message.result, JiaoYuActivityEntity.class);
            final List<JiaoYuActivityEntity.SlideBean> slides = jiaoYuActivityEntity.getSlide();
            JiaoYuActivityPageAdapter pageAdapter = new JiaoYuActivityPageAdapter(this,slides);
            viewPager.setAdapter(pageAdapter);
            pageAdapter.notifyDataSetChanged();
            textTitle.setText(slides.get(0).getTitle());
            viewPager.addOnPageChangeListener(new MyViewPageListener() {
                @Override
                public void onPageSelected(int position) {
                    i = position;
                    position %= slides.size();
                    if (position < 0) {
                        position = slides.size() + position;
                    }
                    viewPager.setCurrentItem(i);
                    textTitle.setText(slides.get(position).getTitle());
                }
            });

            listBeans.addAll(jiaoYuActivityEntity.getList());
            baseAdapter.notifyDataSetChanged();
            listView.onRefreshComplete();
        }
    }
}
